package nl.jworks.epub.organizer.services;

import nl.jworks.epub.organizer.dao.BookDao;
import nl.jworks.epub.organizer.domain.Author;
import nl.jworks.epub.organizer.domain.Book;
import nl.jworks.epub.organizer.repository.AuthorRepository;
import nl.jworks.epub.organizer.repository.BookRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class AuthorService {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private BookRepository bookRepository;

    @Autowired
    private AuthorRepository authorRepository;

    public Book addBook(Book book) {
        log.debug("Adding book : {}", book);
        Book persistedBook = bookRepository.save(book);

        log.debug("Number of books: {}", bookRepository.count());

        return persistedBook;
    }

    public Author addAuthor(Author author) {
        log.debug("Adding author : {}", author);
        Author persistedAuthor = authorRepository.save(author);

        log.debug("Number of authors: {}", authorRepository.count());

        return persistedAuthor;
    }

    public List<Book> findByAuthor(Author author) {
        return bookRepository.findByAuthor(author);

    }

    public Iterable<Book> findAll() {
        return bookRepository.findAll();
    }
}
